package util;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import org.springframework.core.io.support.PropertiesLoaderUtils;

import com.gexin.rp.sdk.base.IPushResult;
import com.gexin.rp.sdk.base.impl.AppMessage;
import com.gexin.rp.sdk.base.impl.ListMessage;
import com.gexin.rp.sdk.base.impl.Target;
import com.gexin.rp.sdk.base.payload.APNPayload;
import com.gexin.rp.sdk.base.uitls.AppConditions;
import com.gexin.rp.sdk.http.IGtPush;
import com.gexin.rp.sdk.template.TransmissionTemplate;

public class PushUtil {
	
	/*AppID： 
	B6GhfxSyVZ5R9R0cZXmDv
	AppSecret： 
	1vBsNfCZhY878qNa9lYZW3
	AppKey： 
	dAqAVRbtLM6Ffa3lgwkQR4
	MasterSecret： 
	GQ1Nzxggj98QWLGPRiz1b6重置*/
	
	/*2018.0524.修改了安卓推送的key 和ios用统一参数。不用再来回切换了*/
	
	public static String appId = null;
	public static String appKey = null;
	public static String masterSecret = null;
	public static String host =  null;
	
	
	public static void getuiPushSingle(String cid, String logo,String title, String content, String url, String json) {
		if(cid == null){
			return;
		}
//		url = "http://www.baidu.com";
		List<String> cids = new ArrayList<String>();
		cids.add(cid);
		try {
			getuiPushList(cids, logo, title, content,json);
		} catch (Exception e) {
			e.printStackTrace();
		}
	        /*IGtPush push = new IGtPush(host, appKey, masterSecret);
	        LinkTemplate template = linkTemplateDemo( logo, title,  content,  url);
	        SingleMessage message = new SingleMessage();
	        message.setOffline(true);
	        // 离线有效时间，单位为毫秒，可选
	        message.setOfflineExpireTime(24 * 3600 * 1000);
	        message.setData(template);
	        // 可选，1为wifi，0为不限制网络环境。根据手机处于的网络情况，决定是否下发
	        message.setPushNetWorkType(0); 
	        Target target = new Target();
	        target.setAppId(appId);
	        target.setClientId(cid);
	        //target.setAlias(Alias);
	        IPushResult ret = null;
	        try {
	            ret = push.pushMessageToSingle(message, target);
	        } catch (RequestException e) {
	            e.printStackTrace();
	            ret = push.pushMessageToSingle(message, target, e.getRequestId());
	        }*/
//	        if (ret != null) {
//	            System.out.println(ret.getResponse().toString());
//	        } else {
//	            System.out.println("服务器响应异常");
//	        }
	    }
	
	/*public static void getuiPushAll(String logo,String title, String content, String url) throws Exception{
		url = "http://www.baidu.com";
		IGtPush push = new IGtPush(host, appKey, masterSecret);

        // 定义"点击链接打开通知模板"，并设置标题、内容、链接
		LinkTemplate template = linkTemplateDemo( logo, title,  content,  url);
        template.setAppId(appId);
        template.setAppkey(appKey);

        List<String> appIds = new ArrayList<String>();
        appIds.add(appId);

        // 定义"AppMessage"类型消息对象，设置消息内容模板、发送的目标App列表、是否支持离线发送、以及离线消息有效期(单位毫秒)
        AppMessage message = new AppMessage();
        message.setData(template);
        message.setAppIdList(appIds);
        message.setOffline(true);
        message.setOfflineExpireTime(24 * 1000 * 3600);

        IPushResult ret = push.pushMessageToApp(message);
//        System.out.println(ret.getResponse().toString());
	}*/
	
	public static void getuiPushList(List<String> cids, String logo, String title, String content, String json) {
		if(cids==null || cids.size()==0){
			return;
		}
		 // 配置返回每个用户返回用户状态，可选
        System.setProperty("gexin_pushList_needDetails", "true");
    // 配置返回每个别名及其对应cid的用户状态，可选
    // System.setProperty("gexin_pushList_needAliasDetails", "true");
        IGtPush push = new IGtPush(host, appKey, masterSecret);
        // 通知透传模板
       TransmissionTemplate template = getTemplate(logo, title, content, json);
        ListMessage message = new ListMessage();
        message.setData(template);
        // 设置消息离线，并设置离线时间
        message.setOffline(true);
        // 离线有效时间，单位为毫秒，可选
        message.setOfflineExpireTime(24 * 1000 * 3600);
        // 配置推送目标
        List targets = new ArrayList();
        for (String cid : cids) {
        	Target target = new Target();
        	target.setAppId(appId);
//        	target.setClientId(cid);
        	target.setAlias(cid);
        	targets.add(target);
		}
        // taskId用于在推送时去查找对应的message
        String taskId = push.getContentId(message);
        IPushResult ret = push.pushMessageToList(taskId, targets);
//        System.out.println(ret.getResponse().toString());
        try {
			push.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static TransmissionTemplate getTemplate(String logo, String title, String content, String json) {
	    TransmissionTemplate template = new TransmissionTemplate();
	    template.setAppId(appId);
	    template.setAppkey(appKey);
	    template.setTransmissionContent(json);
	    template.setTransmissionType(2);
	    APNPayload payload = new APNPayload();
	    //在已有数字基础上加1显示，设置为-1时，在已有数字上减1显示，设置为数字时，显示指定数字
	    payload.setAutoBadge("1");
	    payload.setContentAvailable(1);
	    payload.setSound("default");
	    payload.setCategory("$由客户端定义");
	    payload.addCustomMsg("data", json);
	    //简单模式APNPayload.SimpleMsg
	    payload.setAlertMsg(getDictionaryAlertMsg(logo, title, content, json));

	    //字典模式使用APNPayload.DictionaryAlertMsg
	    //payload.setAlertMsg(getDictionaryAlertMsg());

	    // 添加多媒体资源
//	    payload.addMultiMedia(new MultiMedia().setResType(MultiMedia.MediaType.video)
//	                .setResUrl("http://ol5mrj259.bkt.clouddn.com/test2.mp4")
//	                .setOnlyWifi(true));

	    template.setAPNInfo(payload);
	    return template;
	}
	
	private static APNPayload.DictionaryAlertMsg getDictionaryAlertMsg(String logo, String title, String content, String json){
	    APNPayload.DictionaryAlertMsg alertMsg = new APNPayload.DictionaryAlertMsg();
	    alertMsg.setBody(content);
	    alertMsg.setActionLocKey("ActionLockey");
	    alertMsg.setLocKey("LocKey");
	    alertMsg.addLocArg("type");
	    alertMsg.setLaunchImage("launch-image");
	    // iOS8.2以上版本支持
//	    alertMsg.setTitle(title);
//	    alertMsg.setTitleLocKey("TitleLocKey");
	    alertMsg.addTitleLocArg("TitleLocArg");
	    return alertMsg;
	}

	public static void getuiPushAll(String logo,String title, String content,String json)  {
		  IGtPush push = new IGtPush(host, appKey, masterSecret);

		  TransmissionTemplate template = getTemplate(logo, title, content, json);
	        AppMessage message = new AppMessage();
	        message.setData(template);

	        message.setOffline(true);
	        //离线有效时间，单位为毫秒，可选
	        message.setOfflineExpireTime(24 * 1000 * 3600);
	        //推送给App的目标用户需要满足的条件
	        AppConditions cdt = new AppConditions(); 
	        List<String> appIdList = new ArrayList<String>();
	        appIdList.add(appId);
	        message.setAppIdList(appIdList);
	        //手机类型
	        List<String> phoneTypeList = new ArrayList<String>();
	        //省份
	        List<String> provinceList = new ArrayList<String>();
	        //自定义tag
	        List<String> tagList = new ArrayList<String>();

	        cdt.addCondition(AppConditions.PHONE_TYPE, phoneTypeList);
	        cdt.addCondition(AppConditions.REGION, provinceList);
	        cdt.addCondition(AppConditions.TAG,tagList);
	        message.setConditions(cdt); 

	        IPushResult ret = push.pushMessageToApp(message,"任务别名_toApp");
	        try {
				push.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
	}
	public static void main(String[] args) {
		System.out.println(appId);
		System.out.println(appKey);
		System.out.println(masterSecret);
		System.out.println(host);
	}
}
